// miniprogram/pages/player/player.js
const backgroundAudioManager = wx.getBackgroundAudioManager()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    playing:true,
    musicTime:'',
    timeValue:'0',
    replayStatus:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.getStorage({
      key: 'replay',
      success (res) {
        console.log(res.data)
        if(res.data == "false"){
          that.setData({
            replayStatus:false
          })
        }else{
          that.setData({
            replayStatus:true
          })
        }
      },
      fail(error){
        wx.setStorage({
          data: "false",
          key: 'replay',
        })
      }
    })
    
    let ID = options.ID
    var indexNum = options.indexNum
    const db = wx.cloud.database()
    const albumListDB = db.collection('musicList')
    albumListDB.doc(ID).get({
      success: function (res) {
        var object = res.data
        that.setData({
          titleIcon:object.albumIcon,
          titleName:object.musicDic.musicList[indexNum],
          indexNum:indexNum,
          object:object
        })
        backgroundAudioManager.title = object.musicDic.musicList[indexNum]
        backgroundAudioManager.epname = object.musicDic.musicList[indexNum]
        backgroundAudioManager.singer = '周杰伦'
        backgroundAudioManager.coverImgUrl = object.albumIcon
        backgroundAudioManager.src = object.musicDic.playUrlList[indexNum]
        backgroundAudioManager.onPlay(() => {
          console.log('开始播放')
          console.log(backgroundAudioManager.duration)
          var duration = backgroundAudioManager.duration;
          var min = parseInt(duration / 60);
          var sec = parseInt(duration % 60);
          if (min.toString().length == 1) {
            min = `0${min}`;
          }
          if (sec.toString().length == 1) {
            sec = `0${sec}`;
          }
          var timeStr = min+':'+sec
          that.setData({
            playing:true,
            musicTime:timeStr
          })
        })
        
      }
    })
    backgroundAudioManager.onPause(() =>{
      that.setData({
        playing:false
      })
    })
    backgroundAudioManager.onError((res) => {
      that.setData({
        playing:false
      })
    })
    backgroundAudioManager.onWaiting((res) =>{
      wx.showToast({
        title: '数据加载中......',
        icon:'none'
      })
    })
    backgroundAudioManager.onEnded((res) =>{
      if(that.data.replayStatus){
        // const backgroundAudioManager = wx.getBackgroundAudioManager()
        backgroundAudioManager.src = that.data.object.musicDic.playUrlList[that.data.indexNum]
        backgroundAudioManager.title = that.data.object.musicDic.musicList[that.data.indexNum]
        backgroundAudioManager.epname = that.data.object.musicDic.musicList[that.data.indexNum]
        backgroundAudioManager.singer = '周杰伦'
        backgroundAudioManager.coverImgUrl = that.data.object.albumIcon
      }else{
        that.playNext()
      }
    })
    //iOS 上点击下一曲
    backgroundAudioManager.onNext((res) => {
      that.playNext()
    })
    //iOS 上点击上一曲
    backgroundAudioManager.onPrev((res) => {
      that.playPrev()
    })
    backgroundAudioManager.onTimeUpdate((res) =>{
      var duration = backgroundAudioManager.currentTime;
      var min = parseInt(duration / 60);
      var sec = parseInt(duration % 60);
      if (min.toString().length == 1) {
        min = `0${min}`;
      }
      if (sec.toString().length == 1) {
        sec = `0${sec}`;
      }
      var timeStr = min+':'+sec
      var value = backgroundAudioManager.currentTime/backgroundAudioManager.duration * 100
      // console.log(value)
      that.setData({
        currentTime:timeStr,
        timeValue:value
      })
    })
  },

  seekAct(e){
    var timeSeek = e.detail.value/100.0
    // const backgroundAudioManager = wx.getBackgroundAudioManager()
    backgroundAudioManager.seek(timeSeek * backgroundAudioManager.duration)
  },

  playPause(){
    var that = this
    var object = that.data
    // const backgroundAudioManager = wx.getBackgroundAudioManager()
    if (object.playing) {
      backgroundAudioManager.pause()
    }else{
      backgroundAudioManager.play()
    }
    // const innerAudioContext = getApp().globalData.audioPlay
    // if (object.playing) {
    //   innerAudioContext.pause()
    // }else{
    //   innerAudioContext.play()
    // }
  },

  playPrev(){
    var that = this
    var object = that.data.object
    var data = that.data
    // const backgroundAudioManager = wx.getBackgroundAudioManager()
    backgroundAudioManager.stop()
    var indexNum = parseInt(data.indexNum)
    if(indexNum == 0){
      indexNum = object.musicDic.musicList.length - 1
    }else{
      indexNum = parseInt(indexNum - 1)
    }
    backgroundAudioManager.src = object.musicDic.playUrlList[indexNum]
    backgroundAudioManager.title = object.musicDic.musicList[indexNum]
    backgroundAudioManager.epname = object.musicDic.musicList[indexNum]
    backgroundAudioManager.singer = '周杰伦'
    backgroundAudioManager.coverImgUrl = object.albumIcon
    that.setData({
      titleName:object.musicDic.musicList[indexNum],
      indexNum:indexNum
    })
  },

  playNext: function () {
    var that = this
    var object = that.data.object
    var data = that.data
    // const backgroundAudioManager = wx.getBackgroundAudioManager()
    backgroundAudioManager.stop()
    var indexNum = parseInt(data.indexNum)
    if(indexNum == object.musicDic.musicList.length - 1){
      indexNum = 0
    }else{
      indexNum = parseInt(indexNum + 1)
    }
    console.log(indexNum)
    backgroundAudioManager.src = object.musicDic.playUrlList[indexNum]
    backgroundAudioManager.title = object.musicDic.musicList[indexNum]
    backgroundAudioManager.epname = object.musicDic.musicList[indexNum]
    backgroundAudioManager.singer = '周杰伦'
    backgroundAudioManager.coverImgUrl = object.albumIcon
    that.setData({
      titleName:object.musicDic.musicList[indexNum],
      indexNum:indexNum
    })
  },

  rePlay(){
    var that = this
    var data = that.data
    that.setData({
      replayStatus:!data.replayStatus
    })
    // const backgroundAudioManager = wx.getBackgroundAudioManager()
    if(data.replayStatus){
      wx.setStorage({
        data: "true",
        key: 'replay',
      })
    }else{
      wx.setStorage({
        data: "false",
        key: 'replay',
      })
    }
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})